{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f7f55c22",
   "metadata": {},
   "source": [
    "------------\n",
    "* 2022/10/22/week 09\n",
    "* Python学习记录\n",
    "* 记录人：wu-huichun\n",
    "-----------"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9367952e",
   "metadata": {},
   "source": [
    "# 字典（大括号）\n",
    "\n",
    "### 一.字典存储键/值对"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9a51e204",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Name': 'Huichun',\n",
       " 'Gender': 'Female',\n",
       " 'Occupation': 'Student',\n",
       " 'Home Planet': 'Shantou'}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person3 = {'Name': 'Huichun',\n",
    "            'Gender': 'Female',\n",
    "            'Occupation': 'Student',\n",
    "            'Home Planet': 'Shantou'}\n",
    "person3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b85e74ee",
   "metadata": {},
   "source": [
    "### 二.用中括号找值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "87e6aaed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Huichun'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person3['Name']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b5c688f9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Female'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person3['Gender']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8d1bb55d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "person3['Age'] = 20  # 增加新数据，将一个对象赋至一个新键\n",
    "person3['Age']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e3acc27",
   "metadata": {},
   "source": [
    "### 三.频度计数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4e842fd1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found = {}\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "cb13c30d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found['a'] = 0\n",
    "found['e'] = 0\n",
    "found['i'] = 0\n",
    "found['o'] = 0\n",
    "found['u'] = 0\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "ebe27fce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 1, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found['e'] = found['e'] + 1\n",
    "found"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8618c523",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 2, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "found['e'] += 1\n",
    "found"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4fbcf4e4",
   "metadata": {},
   "source": [
    "### 四.迭代处理键和值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1d75ca29",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a was found 0 time(s).\n",
      "e was found 2 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "for k in found:  # 输出无序\n",
    "    print(k, 'was found', found[k], 'time(s).') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "df7eede7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a was found 0 time(s).\n",
      "e was found 2 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "for k in sorted(found):  # 输出有序\n",
    "    print(k, 'was found', found[k], 'time(s).') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "13f307a8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a was found 0 time(s).\n",
      "e was found 2 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "for k, v in sorted(found.items()):  # 输出有序,使用items迭代处理字典\n",
    "    print(k, 'was found', v, 'time(s).') "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3140490",
   "metadata": {},
   "source": [
    "### 五. P113 测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "476206e9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:wu, hui, chun\n",
      "a was found 0 time(s).\n",
      "e was found 0 time(s).\n",
      "i was found 1 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 3 time(s).\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "\n",
    "found = {}\n",
    "\n",
    "found['a'] = 0\n",
    "found['e'] = 0\n",
    "found['i'] = 0\n",
    "found['o'] = 0\n",
    "found['u'] = 0\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found[letter] += 1\n",
    "for k, v in sorted(found.items()):\n",
    "    print(k, 'was found', v, 'time(s).')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25795571",
   "metadata": {},
   "source": [
    "### 六.初始化元音的频度计数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ba8a485a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:hui\n"
     ]
    },
    {
     "ename": "KeyError",
     "evalue": "'u'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Input \u001b[1;32mIn [1]\u001b[0m, in \u001b[0;36m<cell line: 6>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m letter \u001b[38;5;129;01min\u001b[39;00m word:\n\u001b[0;32m      7\u001b[0m     \u001b[38;5;28;01mif\u001b[39;00m letter \u001b[38;5;129;01min\u001b[39;00m vowels:\n\u001b[1;32m----> 8\u001b[0m         found[letter] \u001b[38;5;241m+\u001b[39m\u001b[38;5;241m=\u001b[39m \u001b[38;5;241m1\u001b[39m\n\u001b[0;32m      9\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m k, v \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28msorted\u001b[39m(found\u001b[38;5;241m.\u001b[39mitems()):\n\u001b[0;32m     10\u001b[0m     \u001b[38;5;28mprint\u001b[39m(k, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mwas found\u001b[39m\u001b[38;5;124m'\u001b[39m, v, \u001b[38;5;124m'\u001b[39m\u001b[38;5;124mtime(s).\u001b[39m\u001b[38;5;124m'\u001b[39m)\n",
      "\u001b[1;31mKeyError\u001b[0m: 'u'"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "\n",
    "found = {}\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found[letter] += 1\n",
    "for k, v in sorted(found.items()):\n",
    "    print(k, 'was found', v, 'time(s).')\n",
    "\n",
    "# 字典的键没有初始化，会出现“KeyError”"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d77afe7",
   "metadata": {},
   "source": [
    "* 避免运行时出现KeyError（用in操作符来避免在访问不存在的键时产生KeyError）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f992cab0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruits = {}\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "72a8a720",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruits['apples'] = 10\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2d922023",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'apples' in fruits"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9410cbc9",
   "metadata": {},
   "source": [
    "* 用 “in” 检查成员关系"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ebda8fd2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 1}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if 'bananas' in fruits:\n",
    "    fruits['bananas'] += 1  # 存在，值增1\n",
    "else:\n",
    "    fruits['bananas'] = 1   # 不存在，初始化为1\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3035b7fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 2}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if 'bananas' in fruits:\n",
    "    fruits['bananas'] += 1  \n",
    "else:\n",
    "    fruits['bananas'] = 1   \n",
    "fruits\n",
    "\n",
    "# bananas 已存在，因此值增1，变成2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a4d94f1",
   "metadata": {},
   "source": [
    "* 用 “not in” 替换 “in”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "4d2a60f6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 2, 'pears': 1}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if 'pears' not in fruits:\n",
    "    fruits['pears'] = 0    # 初始化为0\n",
    "fruits['pears'] += 1\n",
    "fruits"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a80f9dad",
   "metadata": {},
   "source": [
    "* 使用 “setdefault” 方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "fbec999a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 2, 'pears': 2}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruits.setdefault('pears', 0)\n",
    "fruits['pears'] += 1\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "7867752b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Provide a word to search for vowels:hui,chun\n",
      "i was found 1 time(s).\n",
      "u was found 2 time(s).\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input(\"Provide a word to search for vowels:\")\n",
    "\n",
    "found = {}\n",
    "\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found.setdefault(letter, 0)\n",
    "        found[letter] += 1\n",
    "for k, v in sorted(found.items()):\n",
    "    print(k, 'was found', v, 'time(s).')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17985320",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
